package com.gps.web.dao.sheet;

import java.sql.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.gps.web.comm.dao.BaseDaoBean;
import com.gps.web.entity.sheets.GpsDevice;

@Repository
public class GpsDeviceDaoBean extends BaseDaoBean<GpsDevice, String> implements
		GpsDeviceDao {

	@Override
	@Transactional
	public List<GpsDevice> getGpsDeviceByPage(Map<String, Object> map) {
		return findByCriteria(getCriteria(map));
	}
	
	private Criteria getCriteria(Map<String, Object> map) {
		Session session = this.getSession();
		Criteria criteria = session.createCriteria(GpsDevice.class);
		
		if(!StringUtils.isBlank((String)map.get("putawayDate"))){
			criteria.add(Restrictions.eq("putawayDate", Date.valueOf(map.get("putawayDate").toString())));
		}
		if(!StringUtils.isBlank((String)map.get("deviceID"))){
			criteria.add(Restrictions.eq("deviceID", map.get("deviceID").toString()));
		}
		if(!StringUtils.isBlank((String)map.get("deviceModelNum"))){
			criteria.add(Restrictions.eq("deviceModelNum", map.get("deviceModelNum").toString()));
		}
		
		return criteria;
	}
}
